package com.sam.im.heiqun.uis.activities;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sam.im.heiqun.R;
import com.sam.im.heiqun.entities.ValidateEntivity;
import com.sam.im.heiqun.nets.PGService;
import com.sam.im.heiqun.utils.ToolsUtils;
import com.yuyh.library.nets.callbacks.AbsAPICallback;
import com.yuyh.library.nets.exceptions.ApiException;
import com.yuyh.library.uis.activitys.BaseSwipeBackActivity;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by wangguodong on 2017/9/13.
 */

public class GroupBrifeEditActivity extends BaseSwipeBackActivity {

    @BindView(R.id.edit_brife)
    EditText edit_brife;
    @BindView(R.id.ok)
    TextView ok;

    private String groupId ;
    private String brife ;
    private PGService mPGservice;

    public static void start(Activity activity, String groupId, String brife){
        Intent intent = new Intent(activity, GroupBrifeEditActivity.class);
        intent.putExtra("groupId",groupId);
        intent.putExtra("brife",brife);
        activity.startActivity(intent);
    }

    @Override
    protected String getTitleText() {
        return getResources().getString(R.string.group_brife);
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        mPGservice = PGService.getInstance();
        ok.setVisibility(View.VISIBLE);

        groupId = getIntent().getStringExtra("groupId");
        brife = getIntent().getStringExtra("brife");

        edit_brife.setText(brife);
    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_group_brife_edit;
    }

    @OnClick({ R.id.pre_v_back, R.id.ok})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.pre_v_back :
                scrollToFinishActivity();
                break;
            case R.id.ok :

                String brf = edit_brife.getText().toString();
                if (null==brf || "".equals(brf)){
                    showToast(getResources().getString(R.string.input_group_brife));
                }else {
                    setGroupBrife(brf);
                }

                break;
        }
    }


    private void setGroupBrife(String brife){
        mPGservice.updateGroupDesc(ToolsUtils.getMyUserId(), groupId, brife).subscribe(new AbsAPICallback<ValidateEntivity>() {
            @Override
            protected void onResultError(ApiException ex) {
                showToast(ex.getDisplayMessage());
            }

            @Override
            public void onNext(ValidateEntivity s) {
                hideProgress();
                showToast(getResources().getString(R.string.set_success));
                finish();
            }
        });
    }

}
